package com.example.mooctao;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Random;
//忘记密码界面
public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {

    private String mVerifycode;
    private EditText et_password_first;
    private EditText et_password_second;
    private EditText et_verifycode;
    private  EditText phones;
    //创建视图
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_forget);
         et_password_first=findViewById(R.id.et_password_first);
         et_password_second=findViewById(R.id.et_password_second);
         et_verifycode=findViewById(R.id.et_verifycode);
         phones=findViewById(R.id.et_forget);
        findViewById(R.id.btn_verifycode).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        phones.addTextChangedListener(new HideTextWatcher(phones,11));
        et_password_first.addTextChangedListener(new HideTextWatcher(et_password_first,6));
        et_password_second.addTextChangedListener(new HideTextWatcher(et_password_second,6));
        et_verifycode.addTextChangedListener(new HideTextWatcher(et_verifycode,6));
        //点击叉号图标退出当前界面
        findViewById(R.id.back_close1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //获取验证码
            case R.id.btn_verifycode:
                //生成六位随机数字的密码
                String phone1=phones.getText().toString();
                mVerifycode = String.format("%06d", new Random().nextInt(999999));
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("请记住验证码");
                builder.setMessage("手机号" + phone1 + "，本次验证码是" + mVerifycode + ",请输入验证码");
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
                //修改完成点击按钮
            case R.id.btn_confirm:
                //点击确定按钮
                String phone=phones.getText().toString();
               String password_first= et_password_first.getText().toString();
               String password_second=et_password_second.getText().toString();
               if(phone.length()<11){
                   Toast.makeText(this,"请输入正确的手机号码",Toast.LENGTH_SHORT).show();
                   return;
               }
               if(password_first.length()<6){
                   Toast.makeText(this,"请输入正确的密码",Toast.LENGTH_SHORT).show();
                   return;
               }
               if(!password_first.equals(password_second)){
                   Toast.makeText(this,"两次输入的新密码不一致",Toast.LENGTH_SHORT).show();
                   return;
               }
               if(!mVerifycode.equals(et_verifycode.getText().toString())){
                   Toast.makeText(this,"请输入正确的验证码",Toast.LENGTH_SHORT).show();
                   return;
               }
               Toast.makeText(this,"密码修改成功",Toast.LENGTH_SHORT).show();
               //以下把修改好的新密码返回给上一个页面
                Intent intent=new Intent();
                intent.putExtra("new_password",password_first);
                intent.putExtra("forget_phone",phone);
                setResult(Activity.RESULT_OK,intent);
                finish();
                 break;

        }
    }
    //文本输入键盘隐藏
    private class HideTextWatcher implements TextWatcher {
        private EditText mView;
        private int mMaxLength;
        private Context mContext;
        public HideTextWatcher(EditText v, int maxLength) {
            this.mView=v;
            this.mMaxLength=maxLength;
            this.mContext = LoginForgetActivity.this;  // 获取当前Activity的Context
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(editable.toString().length()==mMaxLength){
                //隐藏键盘
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null && mView != null) {
                    imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
                }
            }

        }
    }
}
